package za.ac.cput.peterjohn.DesignPatternPrincipals.Behavioral.Observer.Model;

import za.ac.cput.peterjohn.DesignPatternPrincipals.Behavioral.Observer.Model.WeatherObserver;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class WeatherStation implements WeatherSubject
{
    Set<WeatherObserver> weatherObservers;
    int tempreture;

    public WeatherStation( int tempreture) 
    {
        weatherObservers = new HashSet<WeatherObserver>();
        this.tempreture = tempreture;
    }

    
    @Override
    public String addObserver(WeatherObserver weatherObserver) 
    {
        weatherObservers.add(weatherObserver);
        return "Weather Observer added";
    }

    @Override
    public String removeObserver(WeatherObserver weatherObserver) 
    {
        weatherObservers.remove(weatherObserver);
        return "Weather Observer removed";
    }

    @Override
    public String doNotify() 
    {
        Iterator<WeatherObserver> it = weatherObservers.iterator();
        
        while(it.hasNext())
        {
            WeatherObserver weatherObserver = it.next();
            weatherObserver.doUpdate(tempreture);
        }
        
        return "" + tempreture;
    }

    public void setTempreture(int tempreture)
    {
        this.tempreture = tempreture;
        doNotify();
    }

}